package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.OpenShopVo;
import com.platform.entity.ShopVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiShopService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhanghao
 */
@Api(tags = "商铺管理")
@RestController
@RequestMapping("/api/shop")
public class ApiShopController extends ApiBaseAction {

    @Autowired
    private ApiShopService shopService;

    /**
     * 查询商铺列表
     */
    @ApiOperation(value = "查询商铺列表", response = Map.class)
    @PostMapping("list")
    @IgnoreAuth
    public Object queryShopList(@RequestParam Integer buildingId, @RequestParam String type) {

        return toResponsSuccess(shopService.queryShopList(buildingId,type));
    }

    /**
     * 申请开店
     */
    @ApiOperation(value = "申请开店", response = Map.class)
    @PostMapping("openShop")
    public Object openShop(@LoginUser UserVo loginUser) {
        JSONObject json = this.getJsonRequest();
        OpenShopVo entity = JSONObject.toJavaObject(json,OpenShopVo.class);

        entity.setUserId(loginUser.getUserId().intValue());
        int saveFlag = shopService.openShop(entity);
        if (saveFlag == 1){
            return toResponsSuccess(null);
        }else {
            return toResponsFail(null);
        }
    }

    /**
     * 修改店铺基本信息
     */
    @ApiOperation(value = "修改店铺基本信息", response = Map.class)
    @PostMapping("motifyShopInfo")
    public Object motifyShopInfo(@LoginUser UserVo loginUser) {
        JSONObject json = this.getJsonRequest();
        ShopVo entity = JSONObject.toJavaObject(json,ShopVo.class);
        entity.setUserId(loginUser.getUserId().intValue());

        int updateFlag = shopService.motifyShopInfo(entity);
        if (updateFlag == 1){
            return toResponsSuccess(null);
        }else {
            return toResponsFail(null);
        }
    }

    /**
     * 查询店铺信息
     */
    @ApiOperation(value = "查询店铺信息", response = Map.class)
    @PostMapping("queryByUserId")
    public Object queryByUserId(@LoginUser UserVo loginUser) {
        Integer userId = loginUser.getUserId().intValue();
        return toResponsSuccess(shopService.queryByUserId(userId));
    }

}